<script setup lang="ts">
import { ref, reactive, watch, onMounted, onUnmounted, nextTick } from "vue";
import ReTable from "@/components/ReTable/index.vue";
import { EditorUpload } from "@/components/ReEditor";
import {
  ElMessage,
  ElMessageBox,
  type FormInstance,
  type FormRules
} from "element-plus";
import type { Action } from "element-plus";
import ReNewDialog from "@/components/ReNewDialog/index.vue";
import { useSettingStoreHook } from "@/store/modules/settings";
defineOptions({
  name: "agreementList"
});
interface ValueType {
  val: {
    buttonType: string;
  };
  row: object;
}
const pureSetting = useSettingStoreHook();
const dialogCreateVisible = ref(false);
const dialogTitle = ref("");
const ruleFormRef = ref<FormInstance>();
const deleteModal = () => {
  ElMessageBox.confirm("确定要删除选中的协议吗?", "提示", {
    distinguishCancelAndClose: true,
    confirmButtonText: "确定",
    cancelButtonText: "取消"
  })
    .then(() => {
      ElMessage({
        type: "success",
        message: "删除成功"
      });
    })
    .catch((action: Action) => {
      ElMessage({ type: "info", message: "取消删除" });
    });
};
watch(
  () => pureSetting.buttonInfo,
  (newValue, oldValue) => {
    if (Object.keys(newValue).length !== 0) {
      let value = newValue as ValueType;
      console.log(value);
      switch (value.val.buttonType) {
        case "delete":
          deleteModal();
          break;
        case "edit":
          dialogTitle.value = "修改协议";
          dialogCreateVisible.value = true;
          break;
      }
    }
  },
  {
    immediate: true,
    deep: true
  }
);
const agreementForm = reactive({
  crux_key: "", // 关键词
  title: "", // 折扣金额
  sort: 0, // 排序
  content: "" // 协议内容
});
const rules = reactive<FormRules>({
  crux_key: [{ required: true, message: "请输入关键词", trigger: "blur" }],
  title: [{ required: true, message: "请输入协议标题", trigger: "blur" }],
  content: [{ required: true, message: "请输入协议内容", trigger: "blur" }]
});
const handleGetCreate = () => {
  dialogTitle.value = "添加协议";
  dialogCreateVisible.value = true;
};
const handleGetDeleteIds = (deleteIds: string[]) => {
  deleteModal();
};
const handleCloseDialog = () => {
  console.log("关闭弹窗");
  resetForm(ruleFormRef.value);
  dialogTitle.value = "";
};
const handleConfirmDialog = () => {
  console.log("确认弹窗");
  submitForm(ruleFormRef.value);
};
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      resetForm(formEl);
    } else {
      console.log("error submit!", fields);
    }
  });
};
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
  dialogCreateVisible.value = false;
};
// 获取协议内容
const getAgreementContent = (value: string) => {
  agreementForm.content = value;
};
</script>
<template>
  <div class="agreement-list-container">
    <ReTable
      class="content-body"
      type="agreementList"
      create-btn-text="添加平台协议"
      delete-btn-text="删除选中平台协议"
      @delete="handleGetDeleteIds"
      @create="handleGetCreate"
    />
    <ReNewDialog
      :visible="dialogCreateVisible"
      :dialogTitle="dialogTitle"
      @close="handleCloseDialog"
      @confirm="handleConfirmDialog"
    >
      <template #content>
        <el-form
          ref="ruleFormRef"
          :model="agreementForm"
          :rules="rules"
          label-width="140px"
        >
          <el-form-item label="关键词" prop="crux_key">
            <el-input v-model="agreementForm.crux_key" />
          </el-form-item>
          <el-form-item label="标题" prop="title">
            <el-input v-model="agreementForm.title" />
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input v-model="agreementForm.sort" />
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <EditorUpload @onValue="getAgreementContent" />
          </el-form-item>
        </el-form>
      </template>
    </ReNewDialog>
  </div>
</template>
